package org.web.accesscontrollist.server.biz.impl;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.web.accesscontrollist.sdk.biz.AclResourceDefinitionBiz;
import org.web.accesscontrollist.sdk.dto.*;
import org.web.accesscontrollist.server.domain.AclResourceDefinitionDO;
import org.web.accesscontrollist.server.domain.query.QueryAclResourceDefinition;
import org.web.accesscontrollist.server.service.AclResourceDefinitionService;
import org.web.base.domain.ResultDO;
import org.web.base.domain.helper.ResultHelper;
import org.web.base.domain.helper.ServiceExceptionHelper;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("aclResourceDefinitionBiz")
public class AclResourceDefinitionBizImpl implements AclResourceDefinitionBiz {

    private static final Logger logger = Logger.getLogger(AclResourceDefinitionBizImpl.class);

    @Resource
    private AclResourceDefinitionService aclResourceDefinitionService;

    @Resource
    private AclBusinessLineServiceBiz aclBusinessLineServiceBiz;

    @Override
    public ResultDO<List<AclResourceDefinitionDTO>> saveAclResourceDefinitionBatch(SaveAclDefinitionBatchDTO saveAclDefinitionBatchDTO) {
        ResultDO<List<AclResourceDefinitionDTO>> resultDO = new ResultDO<>();
        try {
            // 参数校验
            AclResourceHelper.checkSaveAclResourceDefinitionBatch(saveAclDefinitionBatchDTO);
            resultDO.setDomain(saveAclDefinitionBatchDTO.getAclResourceDefinitionDTOList());
            // 判断业务线businessLine是否存在。 校验业务线是否存在，如果存在，则校验权限； 如果不存在，则构建缺省业务线。
            OperatorDTO operatorDTO = saveAclDefinitionBatchDTO.getOperatorDTO();
            aclBusinessLineServiceBiz.judgeOperatorOrBuildDefaultBusinessLine(operatorDTO);

            for (AclResourceDefinitionDTO aclResourceDefinitionDTO : saveAclDefinitionBatchDTO.getAclResourceDefinitionDTOList()) {
                QueryAclResourceDefinition queryAclResourceDefinition = new QueryAclResourceDefinition();
                queryAclResourceDefinition.setBusinessLine(aclResourceDefinitionDTO.getBusinessLine());
                queryAclResourceDefinition.setDefinitionName(aclResourceDefinitionDTO.getDefinitionName());
                AclResourceDefinitionDO aclResourceDefinitionFromDB = aclResourceDefinitionService.selectOneAclResourceDefinition(queryAclResourceDefinition);
                Date current = new Date();
                if (ObjectUtils.isNotEmpty(aclResourceDefinitionFromDB)) {
                    AclResourceDefinitionDO updateDO = new AclResourceDefinitionDO();
                    BeanUtils.copyProperties(updateDO, aclResourceDefinitionDTO);
                    updateDO.setAclResourceDefinitionId(aclResourceDefinitionFromDB.getAclResourceDefinitionId());
                    updateDO.setCreateTime(null);
                    updateDO.setUpdateTime(current);
                    aclResourceDefinitionService.updateAclResourceDefinitionByAclResourceDefinitionId(updateDO);
                    aclResourceDefinitionDTO.setAclResourceDefinitionId(updateDO.getAclResourceDefinitionId());
                    aclResourceDefinitionDTO.setCreateTime(aclResourceDefinitionFromDB.getCreateTime());
                    aclResourceDefinitionDTO.setUpdateTime(updateDO.getUpdateTime());
                } else {
                    AclResourceDefinitionDO insertDO = new AclResourceDefinitionDO();
                    BeanUtils.copyProperties(insertDO, aclResourceDefinitionDTO);
                    insertDO.setCreateTime(current);
                    insertDO.setUpdateTime(current);
                    aclResourceDefinitionService.insertAclResourceDefinition(insertDO);
                    aclResourceDefinitionDTO.setAclResourceDefinitionId(insertDO.getAclResourceDefinitionId());
                    aclResourceDefinitionDTO.setCreateTime(insertDO.getCreateTime());
                    aclResourceDefinitionDTO.setUpdateTime(insertDO.getUpdateTime());
                }
                aclResourceDefinitionDTO.setExecuteSuccess(true);
            }
        } catch (Exception e) {
            logger.error(ServiceExceptionHelper.getExceptionInfo(e));
            resultDO = ResultHelper.buildResultDOByException(e);
        }
        return resultDO;
    }

}
